<?php 

namespace pmvc\ioc\annotation;

use pmvc\log\Logger;

use pmvc\ioc\ObjectFactoryAware;

use pmvc\ioc\ObjectFactoryUtil;

use pmvc\ioc\ObjectDescriptor;

use pmvc\ioc\ObjectFactory;
use pmvc\ioc\InitializingObject;

use Exception;

use ReflectionClass;

class AutowireDescriptorRegistrar
	implements InitializingObject,
	ObjectFactoryAware {

	/**
	 * @var Logger
	 */
	private $logger;

	/**
	 * @var AutowireCandidateProvider
	 */
	private $provider;

	/**
	 * @var ObjectFactory
	 */
	private $objectFactory;

	/**
	 * Creates the registrar.
	 */
	public function __construct() {
		$this->logger = Logger::getLogger(__CLASS__);
	}

	/**
	 * {@inheritDoc}
	 */
	public function initialize() {

		// get candidates
		foreach ($this->provider->getAutowireCandidates() as $type) {

			// create it
			$this->logger->trace("Create ObjectDescriptor for {0}", $type);
			$desc = ObjectFactoryUtil::createAutowireDescriptorForClass($this->objectFactory, $type);

			// register it
			$this->objectFactory->addObjectDescriptor($desc);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public function setObjectFactory(ObjectFactory $objectFactory) {
		$this->objectFactory = $objectFactory;
	}

	public function setProvider(AutowireCandidateProvider $provider) {
		$this->provider = $provider;
	}

}

?>